package cn.dazhou.wificonnectdemo.wifi

import android.content.IntentFilter
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.net.wifi.WifiManager
import cn.dazhou.wificonnectdemo.App

/** wifi自动连接器 */
object WifiAutoConnector {

    /** 初始化 */
    fun init() {
        App.app.registerReceiver(WifiSwitchReceiver, IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION))
        App.app.registerReceiver(WifiScanResultReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
        val networkRequest = NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build()
        WifiUtil.connectivityManager.registerNetworkCallback(networkRequest, WifiConnectResultCallback)

        if (!WifiUtil.wifiManager.isWifiEnabled) {
            WifiUtil.wifiManager.isWifiEnabled = true
        }
    }

    /** 反初始化 */
    fun unInit() {
        App.app.unregisterReceiver(WifiSwitchReceiver)
        App.app.unregisterReceiver(WifiScanResultReceiver)
        WifiUtil.connectivityManager.unregisterNetworkCallback(WifiConnectResultCallback)
        WifiScanHandler.removeScan()
    }

}